extends Node

# ===============================================

@export var initial_state: PlayerState
@export var player: Player

# ===============================================

var current_state: PlayerState
var states: Dictionary = {}

# ===============================================

func _ready() -> void:
	for child: PlayerState in get_children():
		if child:
			states[child.state] = child
			child.state_transition.connect(__on_state_transition)
			child.player = player
			child.init()
	if initial_state:
		initial_state.enter()
		current_state = initial_state


func _process(delta: float) -> void:
	current_state.process(delta)

# ===============================================

func __on_state_transition(from: PlayerState, to: PlayerState.State):
	if from != current_state:
		return
	var new_state: PlayerState = states[to]
	if not new_state:
		return
	if current_state:
		current_state.exit()
	new_state.enter()
	current_state = new_state
